package org.spider.schedule;

import lombok.extern.slf4j.Slf4j;
import org.spider.cache.ThreadPoolCache;
import org.spider.download.Downloader;
import org.spider.download.httpclient.HttpClientDownloader;
import org.spider.model.DownloadRequest;
import org.spider.model.DownloadResponse;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 15911
 */
@Component
@Slf4j
public class ThreadSchedule {

    @Async
    public void scheduleUltraShortWave() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10));
        for (int j=0; j<10; j++) {
            threadPoolExecutor.execute(() -> {
                while (true) {
                    DownloadRequest downloadRequest = new DownloadRequest();
                    downloadRequest.setUrl("https://www.baidu.com/");
                    Downloader downloader = new HttpClientDownloader();
                    DownloadResponse downloadResponse = downloader.download(downloadRequest);
                    System.out.println(downloadResponse.getStatusCode());
                }
            });
        }
        threadPoolExecutor.shutdown();
        ThreadPoolCache.threadPoolMap.put("", threadPoolExecutor);
    }
}
